package com.auto.oa.org.service.impl;

import java.util.List;
import java.util.Map;

import com.auto.oa.menu.domain.OaMenuRoleUserMap;
import com.auto.oa.menu.service.OaMenuServiceFactory;
import com.auto.oa.org.dao.OaOrgDAOFactory;
import com.auto.oa.org.domain.OaOrgInfo;
import com.auto.oa.org.service.IOaOrgInfoService;
import com.auto.oa.user.domain.OaUserInfo;
import com.auto.oa.user.service.OaUserServiceFactory;
import com.system.util.BeanUtil;
import com.system.util.SessionUtil;

public class OaOrgInfoServiceImpl implements IOaOrgInfoService {

	public synchronized List<OaOrgInfo> queryTreeOaOrgInfo(OaOrgInfo bean) throws Exception {
		Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
		if(null == bean.getAuorgParentGuid() || "".equals(bean.getAuorgParentGuid())){
			//查询登录人所在组织
			String userCode = SessionUtil.getCurrentUserLabel();
			OaUserInfo oaUserInfo = OaUserServiceFactory.oaUserInfoService.getOaUserInfoByUserCode(userCode);
			String auserGuid = oaUserInfo.getAuserGuid();
			
			OaMenuRoleUserMap oaMenuRoleUserMap = new OaMenuRoleUserMap();
			oaMenuRoleUserMap.setAuserGuid(auserGuid);
			List<OaMenuRoleUserMap> roleUserMapList = OaMenuServiceFactory.oaMenuRoleService.queryListOaMenuRoleUserMap(oaMenuRoleUserMap);
			
			param.put("auorgGuidList", roleUserMapList);
		}
		
		//按组织名称排序
		param.put("displayOrder", "AUORG_NAME");
		
		List<OaOrgInfo> resultList = OaOrgDAOFactory.oaOrgInfoDAO.queryOaOrgInfo(param);
		
		return resultList;
	}
	
	public List<OaOrgInfo> queryListOaOrgInfo(OaOrgInfo bean) throws Exception {
		Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
		List<OaOrgInfo> resultList = OaOrgDAOFactory.oaOrgInfoDAO.queryOaOrgInfo(param);
		
		return resultList;
	}

	public synchronized boolean saveOrUpdateOaOrgInfo(OaOrgInfo bean) throws Exception {
		try {
			if(BeanUtil.isObjectNotNull(bean)){
				if(BeanUtil.isBlank(bean.getAuorgGuid())){
					//初始化默认参数 树节点深度和树完整路径
					String auorgParentGuid = bean.getAuorgParentGuid();
					if(!BeanUtil.isBlank(auorgParentGuid)){
						OaOrgDAOFactory.oaOrgInfoDAO.insertOaOrgInfo(bean);
						
						bean = initOaOrgInfo(bean);
						
						saveOrUpdateOaOrgInfo(bean);
						
						return true;
					}else{
						return false;
					}
				}else{
					OaOrgDAOFactory.oaOrgInfoDAO.updateOaOrgInfo(bean);
					
					return true;
				}
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	/**
	 * @说明
	 * 新增时初始化组织对象
	 * 深度字段：父节点深度+1
	 * 完整路径字段：父节点路径+/+当前对象主键
	 * 
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	private OaOrgInfo initOaOrgInfo(OaOrgInfo bean) throws Exception {
		String auorgGuid = bean.getAuorgParentGuid();
		OaOrgInfo oaOrgInfo = getOaOrgInfo(auorgGuid);
		String auorgDepth = oaOrgInfo.getAuorgDepth();
		String auorgFullPath = oaOrgInfo.getAuorgFullPath();
		int newDepth = Integer.parseInt(auorgDepth) + 1;
		String newFullPath = auorgFullPath + "/" + bean.getAuorgGuid();
		
		bean.setAuorgDepth(String.valueOf(newDepth));
		bean.setAuorgFullPath(newFullPath);
		
		return bean;
	}

	public synchronized boolean deleteOaOrgInfo(String guid) throws Exception {
		try {
			if(!BeanUtil.isBlank(guid)){
				OaOrgDAOFactory.oaOrgInfoDAO.deleteOaOrgInfo(guid);
				
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public synchronized boolean deleteOaOrgInfoByOrgCode(String orgCode) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public synchronized boolean checkOrgCodeExists(String orgCode) throws Exception {
		try {
			boolean bool = false;
			
			return bool;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public synchronized OaOrgInfo getOaOrgInfo(String guid) throws Exception {
		try {
			OaOrgInfo oaOrgInfo = null;
			if(!BeanUtil.isBlank(guid)){
				oaOrgInfo = OaOrgDAOFactory.oaOrgInfoDAO.loadOaOrgInfo(guid);
			}
			
			return oaOrgInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public synchronized OaOrgInfo getOaOrgInfoByOrgCode(String orgCode) throws Exception {
		try {
			OaOrgInfo oaOrgInfo = null;
			if(BeanUtil.isNotBlank(orgCode)){
				oaOrgInfo = OaOrgDAOFactory.oaOrgInfoDAO.loadOaOrgInfoByOrgCode(orgCode);
			}
			
			return oaOrgInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

}
